﻿Public Class frmQuanLyPhanQuyen

    Private Sub frmQuanLyPhanQuyen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim listTaiKhoan As New DataTable()
        listTaiKhoan = TaiKhoanBUS.GetAll()
        gvDSTaiKhoan.DataSource = listTaiKhoan

        Dim listLoaiTK As New List(Of LoaiTaiKhoanDTO)
        listLoaiTK = LoaiTaiKhoanBUS.GetAll()
        cbxLoaiTK.DataSource = listLoaiTK
        cbxLoaiTK.ValueMember = "iMaLoaiTaiKhoan"
        cbxLoaiTK.DisplayMember = "strTenLoaiTaiKhoan"

        cbxTimLoaiTK.DataSource = listLoaiTK
        cbxTimLoaiTK.ValueMember = "iMaLoaiTaiKhoan"
        cbxTimLoaiTK.DisplayMember = "strTenLoaiTaiKhoan"

        btnLuuLai.Enabled = False
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        txtTenTK.Text = ""
        txtMatKhau.Text = ""
        cbxLoaiTK.Text = ""

        btnLuuLai.Enabled = True
        btnSua.Enabled = False
        btnXoa.Enabled = False
    End Sub

    Private Sub gvDSTaiKhoan_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvDSTaiKhoan.SelectionChanged
        txtTenTK.Text = gvDSTaiKhoan.CurrentRow.Cells(0).Value
        txtMatKhau.Text = gvDSTaiKhoan.CurrentRow.Cells(1).Value
        cbxLoaiTK.Text = gvDSTaiKhoan.CurrentRow.Cells(2).Value
    End Sub

    Private Sub btnLuuLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuuLai.Click
        Dim taikhoan As New TaiKhoanDTO()
        Dim listLoaiTK As New List(Of LoaiTaiKhoanDTO)
        listLoaiTK = LoaiTaiKhoanBUS.GetAll()
        cbxLoaiTK.DataSource = listLoaiTK
        If TaiKhoanBUS.KiemTraDayDu(txtTenTK.Text, txtMatKhau.Text) Then
            taikhoan.strUsername = txtTenTK.Text
            taikhoan.strPassword = txtMatKhau.Text
            taikhoan.iMaLoaiTK = cbxLoaiTK.SelectedValue


            TaiKhoanBUS.Insert(taikhoan)

            MessageBox.Show("Thông tin đã được lưu lại thành công!", "THÔNG BÁO")

            Dim listTaiKhoan As New DataTable()
            listTaiKhoan = TaiKhoanBUS.GetAll()
            gvDSTaiKhoan.DataSource = listTaiKhoan


        Else
            MessageBox.Show("Bạn chưa nhập đủ thông tin cần thiết!", "THÔNG BÁO")
        End If

        btnLuuLai.Enabled = False
        btnSua.Enabled = True
        btnXoa.Enabled = True

    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        If IsNothing(gvDSTaiKhoan.SelectedRows) Then
            MessageBox.Show("Bạn chưa chọn dòng nào để Xóa!!", "THÔNG BÁO")
        Else
            If MessageBox.Show("Bạn có chắc chắn Xóa không?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes Then
                Dim idUsername As String = txtTenTK.Text
                TaiKhoanBUS.delete_TaiKhoan(idUsername)
                MessageBox.Show("Dữ liệu đã được Xóa!!", "THÔNG BÁO")

                Dim listTaiKhoan As New DataTable()
                listTaiKhoan = TaiKhoanBUS.GetAll()
                gvDSTaiKhoan.DataSource = listTaiKhoan
            End If
 

        End If
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim taikhoan As New TaiKhoanDTO()
        If TaiKhoanBUS.KiemTraDayDu(txtTenTK.Text, txtMatKhau.Text) Then
            taikhoan.strUsername = txtTenTK.Text
            taikhoan.strPassword = txtMatKhau.Text
            taikhoan.iMaLoaiTK = cbxLoaiTK.SelectedValue

            TaiKhoanBUS.Update(taikhoan)

            MessageBox.Show("Bạn đã Sửa thành công !!", "THÔNG BÁO")


            Dim listTaiKhoan As New DataTable()
            listTaiKhoan = TaiKhoanBUS.GetAll()
            gvDSTaiKhoan.DataSource = listTaiKhoan
        Else
            MessageBox.Show("Bạn chưa nhập đủ thông tin cần thiết!", "THÔNG BÁO")
        End If
    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        Dim listTaiKhoan As New DataTable()
        listTaiKhoan = TaiKhoanBUS.TimKiemTKTheoLoaiTK(cbxTimLoaiTK.SelectedValue)
        gvDSTaiKhoan.DataSource = listTaiKhoan
    End Sub

    Private Sub btnTatCa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTatCa.Click
        Dim listTaiKhoan As New DataTable()
        listTaiKhoan = TaiKhoanBUS.GetAll()
        gvDSTaiKhoan.DataSource = listTaiKhoan
    End Sub
End Class